#REGION functions

function Display-EndMain(){

Write-Host "##########################################"
Write-Host "# SharePoint Main Script Completed"
	if ($env:restart -eq $true){
	Write-Host -ForegroundColor Red "#: Status: Reboot Required, then re-run Main"
	} 
	elseif ($env:stop -eq $true){
	Write-Host -ForegroundColor Red "#: Status: Deployment had an issue, fix then re-run Main"
	}else {
	Write-Host "# Status: COMPLETED"
	$env:current = "post"
	}
Write-Host "#"
$global:endtime = get-date
Write-Host "# End Time: $global:endtime"
$timespan = New-TimeSpan -Start $global:starttime -End $global:endtime
Write-Host "# Deployment Time: $($timespan.Hours) hours, $($timespan.Minutes) minutes, $($timespan.Seconds) seconds"
Write-Host "##########################################"
}

function Display-StartMain(){

Write-Host "##########################################"
Write-Host "# SharePoint Main Started on $env:computername"
$global:starttime = Get-Date
Write-Host "# Start Time: $global:starttime"
$username = [Environment]::UserDomainName + "\" + [Environment]::UserName
Write-Host "# User: $username"
Write-Host "#"
Write-Host "#"
Write-Host "##########################################"
}

function Start-Logging(){

	## Create Directory
	if (Test-Path "C:\log"){
	}else{
	New-Item -path c:\ -name log -type directory | Out-Null
	}
	
	## Create Directory based on Date
	$dateformat = get-date -format "yyyy-MM-dd"
	if (Test-Path "C:\log\$dateformat"){
	}else{
	New-Item -path c:\log -name $dateformat -type directory | Out-Null
	}
	
	
	## Start Logging
	$hourmin = ""
	$hourmin = get-date -format "yyyy-MM-dd-hhmm"
	Start-Transcript -Path "c:\log\$dateformat\$env:computername-$hourmin-pre.rtf"
}

function Stop-Logging(){
	
	Stop-Transcript
}

#ENDREGION functions


# Main Install
pushd "$env:SPAutoFolder\Main"

function Main(){

Clear-Host

$env:restart = $false
$env:stop = $false
$env:current = "main"

if ($env:logging -eq $false){
} else {
Start-Logging
}

Display-StartMain


$mainScripts = @(

".\CopyBits\copyBits.ps1",                                ## Copies to WAC and MOSS bits to your local computer for installation
".\Moss\InstallMOSS.ps1",								  ## Install MOSS to your local computer
".\Wac\InstallWAC.ps1",									  ## Installs WAC if $env:installWAC is set to $true
".\PsConfig\PSConfig.ps1",								  ## Runs PSConfig on the Farm, Checks if the farm has already been created. If so, tries to connect to the farm
".\WebApp\createWebApp.ps1",							  ## Creates a Generic WebApp for the Farm
".\DefaultSite\createDefaultSite.ps1"					  ## Creates a default Site for WebApp
)

	foreach ($script in $mainScripts){
	$Execute = & $script | Out-Default
		if ($env:restart -eq $true){
		break
		}
		if ($env:stop -eq $true){
		break
		}
	}
	
Display-EndMain

if ($env:logging -eq $false){
} else {
Stop-Logging
}

popd

}

Main
